package ThreadService;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class ClientThread implements Runnable{
        private Socket socket;

        public ClientThread(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try(BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                String msg;
                while ((msg = reader.readLine()) != null) {
                    System.out.println(msg);
                }
            }catch (IOException e) {
                e.printStackTrace();
            }
        }

        public static void main(String[] args) {
            try(Socket socket = new Socket("localhost", 8090)) {
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                new Thread(new ClientThread(socket)).start();
                Scanner scanner = new Scanner(System.in);
                while (scanner.hasNext()) {
                    String command = scanner.nextLine();
                    bw.write(command+"\n");
                    bw.flush();
                }
            }catch (IOException e) {
                e.printStackTrace();
            }
        }

}
